<?php

namespace App\Http\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Space extends Model
{

    protected $table = 'tspace';
    //protected $fillable = ['ftitle', 'fbeginat', 'fmessage', 'fspace', 'fmobile', 'flinkman', 'fcustomerid'];
//    protected $guarded = ['fid'];
//    public static $rule = array(
//        'ftitle' => 'required',
//        'fbeginat' => 'required',
//        'fmessage' => 'required',
//        //'fspace' => 'numeric',
//        'fmobile' => 'required|numeric|min:0',
//        'flinkman' => 'required'
//    );
    public $times = array('08:00-10:00', '10:00-12:00', '12:00-14:00', '14:00-16:00', '16:00-18:00', '18:00-20:00', '20:00-22:00');

    //public $times_second = array('8-10', '10-12', '12-14', '14-16', '16-18', '18-20', '20-22');
    public static $type_zh=array(
                -1=>'作废',
                0=>'过期',
                1=>'可以预定',
                2 =>'已预定',
                3=>'已支付',
                4=>'培训使用',
                5=>'比赛使用',
                6=>'正在使用未支付',
    );

    public function getDatas($data, $STypeID, $SSiteID, $basketball, $soccer)
    {
        $today = date("Y-m-d", time());
        $times = $this->times;
        if ($data == $today) {
            foreach ($times as $key => $value) {
                $temps = explode("-", $value);
                $temp = strtotime($today . ' ' . $temps[0] . ':00');
                if ($temp <= time()) {
                    unset($times[$key]);
                }
            }
        }
        $basketballIDs = array();
        if ($STypeID != 2 && $STypeID != 3) {
            $spaces = self::where('STypeID', $STypeID)->where('SSiteID', $SSiteID)->get();
        } else if ($STypeID == 3) {
            $spaces = self::where('STypeID', $STypeID)->where('SSiteID', $SSiteID)->where('SSoccer', $soccer)->get();
        } else {
            $spaces = self::where('STypeID', $STypeID)->where('SSiteID', $SSiteID)->get();
            if ($basketball == 1) {

            } else if ($basketball == 2) {
                foreach ($spaces as $key => $value) {
                    if ($value->SID == $value->SHalfID) {
                        unset($spaces[$key]);
                    }
                }
                //var_dump($spaces->toArray());
                foreach ($spaces as $value) {
                    $SHalfID = Space::where('SID', $value['SID'])->first()->SHalfID;
                    $temps = Space::where('SHalfID', $SHalfID)->take(2)->get();
                    foreach ($temps as $v) {
                        $basketballIDs[$value['SID']][] = $v->SID;
                    }
                }
            }
        }
        if (count($spaces) <= 0) {
            return '没有场地信息';
        }
        $datas = SpaceOrderDetail::getDatas($data); //获取预定信息
//        echo '<pre>';
//        print_r($basketballIDs);
//        echo '</pre>';
//        die;
        //var_dump(str_replace("-", "", $data));
//        $datatype = json_decode(file_get_contents("http://www.easybots.cn/api/holiday.php?d=" . str_replace("-", "", $data))); //工作日0 休息日1 节假日2
//        foreach ($datatype as $value) {
//            $datatype = $value + 1; //工作日1 休息日2 节假日3
//        }
        $datatype = 1;
//        if ($STypeID == 3) {
//            $prices = DB::table('tcharge')->where('CSTID', $STypeID)->where('CPayTypeDay', $datatype)->where('CSiteID', $SSiteID)->where('CDistinguish', $soccer)->get();
//        } else if ($STypeID == 2) {
//            $prices = DB::table('tcharge')->where('CSTID', $STypeID)->where('CPayTypeDay', $datatype)->where('CSiteID', $SSiteID)->where('CDistinguish', $basketball)->get();
//        } else {
//            $prices = DB::table('tcharge')->where('CSTID', $STypeID)->where('CPayTypeDay', $datatype)->where('CSiteID', $SSiteID)->get();
//        }
        $prices = SpaceOrder::getPrices($datatype, $STypeID, $SSiteID, $basketball, $soccer);
        //die;
        $spaces = $spaces->toArray();
        shuffle($spaces);
        $html = '<table class="" style="width:100%;">';
        //$html.='<tr><th></th>';
//        foreach ($spaces as $value) {
//            $html .= '<th>' . $value['SName'] . '</th>';
//        }
//        $html .= '</tr>';
        foreach ($times as $value) {
            $html .= '<tr><th>' . $value . '</th>';
            foreach ($spaces as $v) {

                $price = $this->getPrice($value, $prices);
                //die;
                if ($STypeID == 2 && $basketball == 2) {
                    //var_dump($v['SID']);
                    //var_dump($basketballIDs[$v['SID']]);
                    //die;
                    if (SpaceOrderDetail::isBasketballOrder($basketballIDs[$v['SID']], $value, $datas)) {
                        $html .= '<td><input class="mytest" type="checkbox" name="orders[]" data-space="' . $v['SID'] . '" data-name="' . $v['SName'] . '" data-price="' . $price . '" value="' . $value . '" id="' . $v['SID'] . $value . '"/><label for="' . $v['SID'] . $value . '">&nbsp;</label></td>';
                    } else {
                        $html .= '<td><input class="mytest" type="checkbox" name="orders[]" data-space="' . $v['SID'] . '" data-name="' . $v['SName'] . '" data-price="' . $price . '" value="' . $value . '" disabled id="' . $v['SID'] . $value . '"/><label for="' . $v['SID'] . $value . '">&nbsp;</label></td>';
                    }
                } else {
                    if (SpaceOrderDetail::isOrder($v['SID'], $value, $datas)) {
                        $html .= '<td><input class="mytest" type="checkbox" name="orders[]" data-space="' . $v['SID'] . '" data-name="' . $v['SName'] . '" data-price="' . $price . '" value="' . $value . '" id="' . $v['SID'] . $value . '"/><label for="' . $v['SID'] . $value . '">&nbsp;</label></td>';
                    } else {
                        $html .= '<td><input class="mytest" type="checkbox" name="orders[]" data-space="' . $v['SID'] . '" data-name="' . $v['SName'] . '" data-price="' . $price . '" value="' . $value . '" disabled id="' . $v['SID'] . $value . '"/><label for="' . $v['SID'] . $value . '">&nbsp;</label></td>';
                    }
                }
            }
            $html .= '</tr>';
        }
        $html .= '</table>';
        return $html;
    }

    public function getList()
    {
        return Fight::where('fbeginat', '>=', date('Y-m-d', time()))->orderBy('fbeginat')->paginate(5);
    }

    //我的场地
    public function getMylist()
    {
        return SpaceOrder::where('SOCustomerID', \Illuminate\Support\Facades\Session::get('customer_id'))->orderBy('SOID', 'desc')->paginate(5);
    }

    public function getPrice($date, $datas)
    {
        $dates = explode('-', $date);
//        echo '<pre>';
//        print_r($dates);
//        echo '</pre>';
//        die;
        if ($dates[1] == '22:00') {
            $dates[1] = '21:59';
        }
        foreach ($dates as $key => $value) {
            $dates[$key] .= ':00';
        }
        //$date .= ':00';
        $price = 0;
        foreach ($dates as $value) {
            $temp = 0;
            foreach ($datas as $v) {
                if ($value >= $v->CBeginAt && $value < $v->CEndAt) {
                    $temp = $v->CUnitPrice;
                }
            }
            $price += $temp;
        }
        return $price;
    }
    //
    public static function getSiteName($id)
    {
        $site=DB::table('TSite')->where('SID',$id)->first();
        if($site)
        {
            return $site->SName;
        }else{
            return '';
        }

    }


}
